package com.qf.book.impl;

import com.qf.book.Book;
import com.qf.book.BookInterface;

import java.util.Scanner;

public class BookImpl implements BookInterface {
    private Book [] library = new Book[3];
    //初始化图书馆，默认放3本书
    private int count = 0;
    //初始化图书馆中书的数量 这个变量也可以作为插入书的下标

    public Book[] getLibrary() {
        return library;
    }

    public void setLibrary(Book[] library) {
        this.library = library;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public boolean addBook(Book book) {
        if (book == null){
            return false;
        }else {
            if (count== library.length){//数组空间不足
                Book [] newLibrary = new Book[library.length*2];
                System.arraycopy(library,0,newLibrary,0,library.length);
                this.library = newLibrary;
            }
            for (int i = 0; i < library.length; i++) {
                if (library[i] != null &&book.getBookId().equals(library[i].getBookId())){
                    System.out.println(book.getBookName()+"添加失败，已存在");
                    return false;
                }
            }

                library[count] = book; //把书放到数组中
            System.out.println(book.getBookName()+"添加成功");
                count++;
                return true;
        }
    }
    @Override
    public boolean updateBook(Book book) {
        for (int i = 0; i < count; i++) {
            if (book.getBookId().equals(library[i].getBookId())){
                library[i] = book;
                System.out.print(book.getBookName() + "更改后的信息为：");
                System.out.println("ID"+library[i].getBookId()+",书名："+ library[i].getBookName()+",作者："+library[i].getAuthor()+",出版社："+library[i].getPublisher()+",页数"+library[i].getPageNumber());
               return true;
            }
        }
        System.out.println(book.getBookName()+"此书不存在");
        return false;
    }

    @Override
    public boolean deleteBook(String bookId) {
        for (int i = 0; i < count; i++) {
            if (bookId.equals(library[i].getBookId())){
                System.out.println(library[i].getBookName()+"删除成功");
                count--;
                System.arraycopy(library,i+1,library,i,library.length-i-1);
                return true;
            }
        }
        return false;
    }

    @Override
    public Book selectOne(String bookId) {
        for (int i = 0; i < count; i++) {
            if (bookId.equals(library[i].getBookId())){
                System.out.println("检索成功,信息为：ID"+library[i].getBookId()+",书名："+ library[i].getBookName()+",作者："+library[i].getAuthor()+",出版社："+library[i].getPublisher()+",页数"+library[i].getPageNumber());
                return library[i];
            }
        }
        System.out.println("检索失败");
        return null;
    }

    @Override
    public Book[] selectAll() {
        for (int i = 0; i < count; i++) {
            System.out.println("第"+(i+1)+",本书的信息为：ID"+library[i].getBookId()+",书名："+ library[i].getBookName()+",作者："+library[i].getAuthor()+",出版社："+library[i].getPublisher()+",页数"+library[i].getPageNumber());
        }
        return new Book[0];
    }
}
